package com.zjsru.controller;

import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.exception.NacosException;
import com.zjsru.pojo.Order;
import com.zjsru.pojo.User;
import com.zjsru.service.OrderService;
import com.zjsru.service.OrderServiceOpenFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/order")
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
    
    @Autowired
    private OrderService orderService;  // 正确的写法：@Autowired 直接修饰字段

    @Autowired
    private OrderServiceOpenFeign orderServiceOpenFeign;

    @GetMapping("/{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderid) throws NacosException {
        logger.debug("开始查询订单，ID: {}", orderid);
        Order order = orderService.queryOrderById(orderid);
        User user = orderServiceOpenFeign.getUserById(order.getUser_Id());
        order.setUser(user);
        return order;
    }
    
    // 添加负载均衡测试端点
    @GetMapping("/lb-test")
    public String loadBalancerTest() {
        logger.info("开始测试负载均衡...");
        for (int i = 0; i < 5; i++) {
            logger.info("第{}次调用用户服务", i+1);
            User user = orderServiceOpenFeign.getUserById(1L);
            logger.info("收到用户服务响应: {}", user);
        }
        return "负载均衡测试完成";
    }
}
